function Select_Question() {
    var _this = this;
    var page;
    _this.obj = null;
    _this.templateArr = [];
    _this.questionIds = [];
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.initcourseTree();
        _this.initQuestionTable();
        _this.initEvent(Object);
        _this.tableInit();
    }

    var course_tree_options = {
        zNodes: '',
        rMenu: ''
    };


    _this.setting = {
        view: {
            dblClickExpand: false,
            fontCss: getFontCss
        },
        callback: {
            onClick: function(e,treeId, treeNode){
                _this.onClick(e,treeId, treeNode);
            }
        }
    };

    //点击
    _this.onClick = function(e,treeId, treeNode) {
        $("#courseId").val(treeNode.id);
        select_question.search();
    };
    // 初始化课程树
    _this.initcourseTree = function(){
        var param = {
            "role_id": $.trim($("#roleId").val()),
            "project_id": $.trim($("#projectId").val())
        }
        var url = appPath + '/popup/course_tree';
        var result = _this.ajax(url,param);
        course_tree_options.zNodes = eval(result);
        $.fn.zTree.init($("#course_treeId"), _this.setting, course_tree_options.zNodes);
        //默认选中根节点
        var treeObj = $.fn.zTree.getZTreeObj("course_treeId");
        var rootNodes = treeObj.getNodesByFilter(function(node){return node.level==0;});
        var rootNode = rootNodes[0];
        // 默认选中根节点
        treeObj.selectNode(rootNode);
    }



    _this.initQuestionTable = function() {
        _this.obj = null;
        var list_url = appPath + "/popup/course_question_list";
        page.init("question_form", list_url, "question_list", "question_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            //$("#checkAll").prop("checked",false);
            _this.obj = dataList;
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td width="50">' + (i+1) + '</td>';
              if(_this.selected(item.intId)){
                inner += '<td width="50" class="checked"><input type=\"checkbox\" value="'+item['intId']+'" name="questionBox" data-id="' + item['intId'] + '"></td>';
              }else{
                inner += '<td width="50"><input type=\"checkbox\" value="'+item['intId']+'" name="questionBox" data-id="' + item['intId'] + '"></td>';
              }
                inner += '<td>'+ item['varNo'] + '</td>';
                var str = claerE(item['varContent']);
                inner += '<td width="150"><div class="text-hide" title="'+ str + '"><span>'+ item['varContent'] + '</span></div></td>';
                inner += '<td width="90">'+ item['chrType'] + '</td>';
                inner += '</tr>';
            }

            return inner;
        }
        _this.tableInit();
    };

    //初始化选择后的表格
    _this.tableInit = function(){
        $('#question_list_copy').html('<tr><td colspan="3" class="empty">暂无数据</td></tr>')
    }




    _this.initEvent = function () {

        // 确定
        $("#btn_sure").click(function () {

            parent.self_select_question.submitQuestion(_this.questionIds)  ;

            _this.closeDialog();
        });

        //取消ss
        $("#btn_cancel").click(function () {
            _this.closeDialog();
        });

        //选择试题
        $('#question_list').on('change','input',function(){
            var $this = $(this);
            var flag = $this.prop('checked');
            var id = $this.attr('data-id');
            var index = _this.getIndex(_this.obj, id);
            var template_index = _this.getIndex(_this.templateArr, id);
            if( flag && index != -1  ){

                _this.removeRepart(_this.obj[index])

            }else if( !flag && template_index != -1 ){

                _this.templateArr.splice( template_index,1 );
                _this.questionIds.splice(template_index,1 );
            }
            _this.select_after();
        })
    }

    //关闭对话框
    _this.closeDialog =function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }
    //获取id索引
    _this.getIndex = function(obj, attr){
        var index = -1;
        $.each(obj,function(i, n){
            if( this.intId == attr ){
                index = i;
                return false;
            }
        })
        return index;
    }
    //选择试题后执行
    _this.select_after = function(){
        var tbody = '';
        $.each(_this.templateArr,function(i, n){
            tbody += '<tr >';
            tbody += '<td  width="50">' + (i+1) + '</td>';
            tbody += '<td>'+ this.varNo + '</td>';
            tbody += '<td  width="240">'+ this.chrType + '</td>';
            tbody += '</tr>';
        })
        $('#question_list_copy').html( tbody );
    }

    //搜索试题
    _this.search = function() {
        //$("#checkAll").prop("checked",false);
      $("#varTitle").val($.trim($("#varTitle").val()));
      _this.initQuestionTable();
        //展示选中试题
      _this.select_after();
    };

    //全部
    _this.all = function() {
        var treeObj = $.fn.zTree.getZTreeObj("course_treeId");
        treeObj.cancelSelectedNode();
        $("#courseId").val("");
        $("#varTitle").val("");

        _this.initQuestionTable();
        //展示选中试题
        _this.select_after();
    };
    //全选
    _this.checkAll=function () {
        var flag=$("#all").prop("checked");
        $('tbody input[name="questionBox"]').prop("checked",flag);
        if(flag && _this.obj.length>0){
            $.each(_this.obj,function(i, n){
                _this.removeRepart(this);
            })
        }else if( !flag ){

            $.each(_this.obj,function(i, n){
                var id = this.intId;
                var template_index = _this.getIndex(_this.templateArr, id);
                if(-1!=template_index){
                _this.templateArr.splice( template_index,1 );
                }
            })

        }
        _this.select_after();
    }

      //是否选中
      _this.selected = function(intId){
        for(var m=0; m<_this.templateArr.length; m++){
          if(_this.templateArr[m].intId == intId){
            return true;
          }
        }
        return false;
      }

    //去重
    _this.removeRepart=function (questionData) {
        var flag=true;
        for (var i=0;i<_this.templateArr.length;i++){
            if(_this.templateArr[i].intId == questionData.intId){
                flag=false;
            }
        }
        if(flag){
            _this.templateArr.push(questionData) ;
            _this.questionIds.push(questionData.intId)
        }
    }



    _this.ajax = function(url, param, type){
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data.result;
            }
        });
        return result;
    };

}

var select_question = new Select_Question();


//搜索高亮
function getFontCss(treeId, treeNode){
    return (!!treeNode.highlight)?{'color': "#A60000", 'font-weight':"bold"}:{'color': "#333", 'font-weight':"normal"};
}

//清楚元素标签
function claerE(str){
    return str.replace(/<[^>]+>/g,'');
}